var prefix = api + "/admin/tenant";

$(function () {

  var options = {
    url: api + "/scabpm/bpm/my/approveList",
    listType: 'bpm',
    updateUrl: '/pages/bpm/taskHistory/edit',
    method: 'post',
    modalName: "办理历史",
    columns: [
      {
        field: 'subject',
        title: '流程标题',
      },
      {
        field: 'defName',
        title: '流程名称',
      },
      {
        field: 'nodeName',
        title: '任务名称',
      },
      {
        field: 'status',
        title: '流程状态',
        align: 'center',
        formatter: function (value, row, index) {
          return statusDict(value)
        }
      },
      {
        field: 'creator',
        title: '发起人',
      },
      {
        field: 'approveTime',
        title: '办理时间',
      }, {
        field: 'durMs',
        title: '处理用时',
        formatter: function (value, row, index) {
          return timeLag(value)
        }
      },
      {
        field: 'approveStatus',
        title: '处理结果',
        align: 'center',
        formatter: function (value, row, index) {
          return resultDict(value)
        }
      },
      {
        title: '操作',
        formatter: function (value, row, index) {
          var actions = [];
          var name = '详情【'+row.subject+'】 - 【'+row.nodeName+'】'
          var url = options.updateUrl + '?id='+row.id+'&nodeId='+row.nodeId
          actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editFullBpm(\'' + row.id + '\',\'' + name + '\',\'' + url + '\')"><i class="fa fa-edit"></i>详情</a> ');
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

function statusDict(item){
  var dict = {
    'running': {text:'运行中',color:'label-success'},
    'end': {text:'终止',color:'label-default'},
    'manualend': {text:'人工终止',color:'label-danger'},
    'revoke': {text:'撤销',color:'label-danger'}
  }
  return '<span class="label '+(dict[item] ? dict[item].color : '')+'">'+(dict[item] ? dict[item].text : item)+'</span>'
}

function resultDict(item){
  var dict = {
    'agree': {text:'同意',color:'label-info'},
    'oppose': {text:'反对',color:'label-danger'},
    'reject': {text:'驳回',color:'label-danger'},
    'signNotPass': {text:'会签反对',color:'label-warning'},
    'signPass': {text:'会签通过',color:'label-success'},
    'turn': {text:'转办',color:'label-navy'},
    'manualEnd': {text:'人工终止',color:'label-danger'},
    'recall': {text:'撤回',color:'label-warning'},
    'addDoAgree': {text:'加办结束',color:'label-info'},
    'addDo': {text:'加办中',color:'label-warning'},
    'revoke': {text:'撤销',color:'label-danger'},
    'carbonCopy': {text:'已审阅',color:'label-warning'},
    'recycle': {text:'回收',color:'label-danger'},
  }
  return '<span class="label '+(dict[item] ? dict[item].color : '')+'">'+(dict[item] ? dict[item].text : item)+'</span>'
}

function timeLag(difference,level){
  if(!difference){
    return "";
  }
  var  r ="",
    ////计算出相差天数
    days=Math.floor(difference/(24*3600*1000)),
    //计算出小时数
    leave1=difference%(24*3600*1000),   //计算天数后剩余的毫秒数
    hours=Math.floor(leave1/(3600*1000)),
    //计算相差分钟数
    leave2=leave1%(3600*1000),      //计算小时数后剩余的毫秒数
    minutes=Math.floor(leave2/(60*1000)),
    //计算相差秒数
    leave3=leave2%(60*1000),    //计算分钟数后剩余的毫秒数
    seconds=Math.round(leave3/1000);

  if(isNaN(level)){
    level = 2;
  }
  if(days>0){
    level--;
    r +=days+"天";
    if(level==0)return r;
  }
  if(hours>0){
    level--;
    r +=hours+"时";
    if(level==0)return r;
  }
  if(minutes>0){
    level--;
    r +=minutes+"分";
    if(level==0)return r;
  }
  if(seconds>0){
    level--;
    r +=seconds+"秒";
    if(level==0)return r;
  }

  if(r){
    return r;
  }

  if(leave3){
    return difference+"毫秒";
  }

  return ""
}